import Vue from 'vue'

import appurl_config from './config'
const BaseUrl = appurl_config.appurl;
const tenantId = appurl_config.tenantId;
const appType = appurl_config.appType;

class Request {
	constructor() {}
	getWithToken(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"GET",
				header:{
					'token': uni.getStorageSync('userToken') ? uni.getStorageSync('userToken') : '',
					'X-Tenant-Id': tenantId,
					'X-App-Type': appType
				},
				success(res){
					console.log(res)
					if(res.data.code == 401){
						console.log('未登录')
						uni.navigateTo({
							url: '/pages/login/login'
						});
						return;
					}
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	postWithToken(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"POST",
				header:{
					'token': uni.getStorageSync('userToken') ? uni.getStorageSync('userToken') : '',
					'X-Tenant-Id': tenantId,
					'X-App-Type': appType
					// 'token':Vue.prototype.Token ? Vue.prototype.Token : ''
				},
				dataType: 'json',
				success(res){
					if(res.data.code == 401){
						uni.navigateTo({
							url: '/pages/login/login'
						});
						return;
					}
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	get(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"GET",
				header:{
					'X-Tenant-Id': tenantId,
					'X-App-Type': appType
				},
				success(res){
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	post(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"POST",
				header:{
					'X-Tenant-Id': tenantId,
					'X-App-Type': appType
				},
				success(res){
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	uploadFileImg(obj){
		return new Promise((resolve, reject) => {
			uni.uploadFile({
			    url: BaseUrl + obj.url, //请求地址
			    filePath: obj.data,
			    name: "file",
			    header: {
					'token': uni.getStorageSync('userToken') ? uni.getStorageSync('userToken') : ''
			    },
			    success: (res) => {
					console.log(res)
					let fullurl = JSON.parse(res.data).data.fullurl;
					console.log(fullurl)
					resolve(fullurl);
			    },
			    fail: (err) => {
					reject(err.msg);
			    },
			});
		})
	}
}
export const request = new Request()